package master;

/**
 * 二叉树的最小深度
 *
 * @author chenyw
 * @date 2022/8/6 9:57
 */
public class M111minDepth {
    public int minDepth(TreeNode root) {
        return dfs(root);
    }

    private int dfs(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = dfs(root.left);
        int right = dfs(root.right);
//        return Math.min(left,right) + 1;
        if (left == 0 && right == 0) {
            return 1;
        } else if (left != 0 && right != 0) {
            return Math.min(left,right) + 1;
        } else {
            return Math.max(left,right) + 1;
        }
    }

}
